{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1f8f53fc",
   "metadata": {},
   "source": [
    "好的，我们来分析这个问题。😊\n",
    "\n",
    "| 金额范围（元）         | 说明 / 投资特征               |\n",
    "| --------------- | ----------------------- |\n",
    "| <50,000         | 小散户，参与度低，流动性小，对股价影响弱    |\n",
    "| 50,000–100,000  | 中等散户，开始有一定市场影响力         |\n",
    "| 100,000–200,000 | 中高散户，持股稳定，可能支撑股价        |\n",
    "| 200,000–500,000 | 高散户，参与度高，对股价利好明显        |\n",
    "| >500,000        | 资金量大，接近中小机构规模，对股价支撑作用显著 |\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "55895907",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "2d2c34184777474f937e92a4c472b0ab",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "  0%|          | 0/85 [00:00<?, ?it/s]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "股票代码: 000001\n",
      "最新报告期: 2025-03-31\n",
      "总股本: 194.06亿\n",
      "前十大股东持股总数: 128.10亿\n",
      "股东总数: 504267.0\n",
      "散户持股数量: 65.96亿\n",
      "散户股东数量: 504257.0\n",
      "散户平均持股数量: 1.31万\n",
      "**********\n",
      "散户平均持股金额: 13.08万\n",
      "散户平均持股金额未超过20万 → 中性/偏弱\n",
      "散户投资特征: 中高散户，可能支撑股价\n",
      "**********\n",
      "\n",
      "散户平均持股金额分级规则及投资特征：\n",
      "金额 < 5万       → 小散户，流动性不足\n",
      "金额 5万-10万    → 中等散户，流动性稍微足\n",
      "金额 10万-20万   → 中高散户，可能支撑股价\n",
      "金额 20万-50万   → 高散户，对股价利好明显\n",
      "金额 > 50万      → 超高散户，接近机构规模，利好显著\n"
     ]
    }
   ],
   "source": [
    "import akshare as ak\n",
    "import pandas as pd\n",
    "\n",
    "# ================= 参数 =================\n",
    "stock_code = \"000001\"\n",
    "CURRENT_PRICE = 10  # 示例股价，可用实时价格替换\n",
    "\n",
    "# ================= 工具函数 =================\n",
    "def format_number(val):\n",
    "    \"\"\"\n",
    "    格式化数字：\n",
    "    - <1万: 显示整数\n",
    "    - 1万~1亿: 显示万，保留两位小数\n",
    "    - >=1亿: 显示亿，保留两位小数\n",
    "    \"\"\"\n",
    "    if val >= 1e8:\n",
    "        return f\"{val/1e8:.2f}亿\"\n",
    "    elif val >= 1e4:\n",
    "        return f\"{val/1e4:.2f}万\"\n",
    "    else:\n",
    "        return f\"{val:.0f}\"\n",
    "\n",
    "def retail_influence_level(amount):\n",
    "    \"\"\"\n",
    "    根据散户平均持股金额判断投资特征\n",
    "    \"\"\"\n",
    "    if amount < 50000:\n",
    "        return \"小散户，流动性不足\"\n",
    "    elif amount < 100000:\n",
    "        return \"中等散户，流动性稍微足\"\n",
    "    elif amount < 200000:\n",
    "        return \"中高散户，可能支撑股价\"\n",
    "    elif amount < 500000:\n",
    "        return \"高散户，对股价利好明显\"\n",
    "    else:\n",
    "        return \"超高散户，接近机构规模，利好显著\"\n",
    "\n",
    "# ================= 1️⃣ 获取前十大股东持股数据 =================\n",
    "df_top10 = ak.stock_circulate_stock_holder(symbol=stock_code)\n",
    "df_top10['截止日期'] = pd.to_datetime(df_top10['截止日期'])\n",
    "df_top10['持股数量'] = df_top10['持股数量'].astype(float)\n",
    "\n",
    "# 取最新报告期\n",
    "latest_report_date = df_top10['截止日期'].max()\n",
    "latest_top10 = df_top10[df_top10['截止日期'] == latest_report_date]\n",
    "\n",
    "# 前十大股东持股总数\n",
    "top10_total_shares = latest_top10['持股数量'].sum()\n",
    "\n",
    "# ================= 2️⃣ 获取总股本 =================\n",
    "stock_value_df = ak.stock_value_em(symbol=stock_code)\n",
    "stock_value_df['数据日期'] = pd.to_datetime(stock_value_df['数据日期'])\n",
    "stock_value_df['总股本'] = stock_value_df['总股本'].astype(float)\n",
    "\n",
    "# 匹配最近数据日期 <= 截止日期\n",
    "mask = stock_value_df['数据日期'] <= latest_report_date\n",
    "if mask.any():\n",
    "    total_shares = stock_value_df.loc[mask].sort_values('数据日期').iloc[-1]['总股本']\n",
    "else:\n",
    "    total_shares = None\n",
    "    print(\"没有总股本数据可用\")\n",
    "\n",
    "# ================= 3️⃣ 获取股东总数 =================\n",
    "df_holder_count = ak.stock_main_stock_holder(stock=stock_code)\n",
    "df_holder_count['截至日期'] = pd.to_datetime(df_holder_count['截至日期'])\n",
    "\n",
    "# 匹配最新报告期\n",
    "mask_count = df_holder_count['截至日期'] <= latest_report_date\n",
    "if mask_count.any():\n",
    "    latest_count_row = df_holder_count.loc[mask_count].sort_values('截至日期').iloc[-1]\n",
    "    total_holders = latest_count_row['股东总数']\n",
    "else:\n",
    "    total_holders = None\n",
    "    print(\"没有股东总数数据\")\n",
    "\n",
    "# ================= 4️⃣ 计算散户持股数量 =================\n",
    "retail_shares = total_shares - top10_total_shares if total_shares is not None else None\n",
    "\n",
    "# 散户股东数量\n",
    "retail_count = total_holders - 10 if total_holders is not None else None\n",
    "\n",
    "# 散户平均持股数量\n",
    "avg_retail_shares = retail_shares / retail_count if retail_shares is not None and retail_count > 0 else None\n",
    "\n",
    "# ================= 5️⃣ 散户平均持股金额 =================\n",
    "avg_retail_value = avg_retail_shares * CURRENT_PRICE if avg_retail_shares is not None else None\n",
    "\n",
    "# ================= 6️⃣ 输出结果 =================\n",
    "print(\"股票代码:\", stock_code)\n",
    "print(\"最新报告期:\", latest_report_date.strftime('%Y-%m-%d'))\n",
    "print(\"总股本:\", format_number(total_shares))\n",
    "print(\"前十大股东持股总数:\", format_number(top10_total_shares))\n",
    "print(\"股东总数:\", total_holders)\n",
    "print(\"散户持股数量:\", format_number(retail_shares))\n",
    "print(\"散户股东数量:\", retail_count)\n",
    "print(\"散户平均持股数量:\", format_number(avg_retail_shares))\n",
    "print('**********')\n",
    "print(\"散户平均持股金额:\", format_number(avg_retail_value))\n",
    "# ================= 7️⃣ 判断是否利好 =================\n",
    "if avg_retail_value is not None:\n",
    "    if avg_retail_value > 200000:\n",
    "        print(\"散户平均持股金额超过20万 → 利好\")\n",
    "    else:\n",
    "        print(\"散户平均持股金额未超过20万 → 中性/偏弱\")\n",
    "\n",
    "# ================= 8️⃣ 输出投资特征 =================\n",
    "if avg_retail_value is not None:\n",
    "    print(\"散户投资特征:\", retail_influence_level(avg_retail_value))\n",
    "print('**********')\n",
    "\n",
    "# ================= 9️⃣ 打印散户金额分级规则 =================\n",
    "print(\"\\n散户平均持股金额分级规则及投资特征：\")\n",
    "print(\"金额 < 5万       → 小散户，流动性不足\")\n",
    "print(\"金额 5万-10万    → 中等散户，流动性稍微足\")\n",
    "print(\"金额 10万-20万   → 中高散户，可能支撑股价\")\n",
    "print(\"金额 20万-50万   → 高散户，对股价利好明显\")\n",
    "print(\"金额 > 50万      → 超高散户，接近机构规模，利好显著\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
